//
//  STAgreeServiceView.swift
//  StarTimesON
//
//  Created by Toj on 9/27/22.
//  Copyright © 2022 StarTimes. All rights reserved.
//

import UIKit

class STAgreeServiceView: UIView {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .white
        setupMakeAddSubviews()
        
        let tap = UITapGestureRecognizer(target: self, action: #selector(didTapService))
        serviceLabel.addGestureRecognizer(tap)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        setupMakeLayoutSubviews()
    }
    
    @objc private func didTapService() {
        STSRouter.openWebAppPage(.dvbAutorenewal)
    }
    
    private lazy var agreeCheckBox: UIButton = {
        let view = UIButton()
        view.isUserInteractionEnabled = false
        view.setTitle("agree_with_tos".localized(), for: .normal)
        view.setTitleColor("#666666".to_color, for: .normal)
        view.setImage("commodity_agreeCheckBox_sel".to_image, for: .normal)
        view.setContentHuggingPriority(.dragThatCanResizeScene, for: .horizontal)
        view.titleLabel?.font = UIFont.systemFont(ofSize: 14.f_scale)
        view.sizeToFit()
        return view
    }()
    
    private lazy var serviceLabel: UILabel = {
        let view = UILabel()
        view.isUserInteractionEnabled = true
        let font = UIFont.systemFont(ofSize: 14.f_scale)
        let titleColor = "#0087EB".to_color
        let attributed = "public_text_terms_of_service".localized().to_attributedString(font: font, color: titleColor, lineStyle: .underline)
        view.attributedText = attributed
        view.font = font
        view.textColor = titleColor
        
        return view
    }()
}

// MARK: - 初始化
extension STAgreeServiceView {
    
    private func setupMakeAddSubviews() {
        addSubview(agreeCheckBox)
        addSubview(serviceLabel)
    }
    
    private func setupMakeLayoutSubviews() {
        
        let cwidth = agreeCheckBox.frame.width
        let swidth = serviceLabel.attributedText?.st_stringWidth(maxHeight: 24.ui_scale) ?? 0
        let left = (STScreenWidth - (cwidth + swidth)) * 0.5
        agreeCheckBox.snp.makeConstraints { make in
            make.left.equalTo(left)
            make.height.equalTo(24.ui_scale)
            make.centerY.equalTo(self)
            make.width.equalTo(cwidth)
        }
        
        serviceLabel.snp.makeConstraints { make in
            make.left.equalTo(agreeCheckBox.snp.right).offset(2.ui_scale)
            make.centerY.equalTo(agreeCheckBox)
        }
    }
}
